package com.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domian.mapper.AdChannelMapper;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.mchange.v2.lang.ObjectUtils;
import com.service.AdChannerlService;
import io.netty.util.internal.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdChannerlServiceImpl implements AdChannerlService {

    @Autowired
    private AdChannelMapper adChannelMapper;
    ResponseResult responseResult = new ResponseResult();


    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        QueryWrapper<AdChannel> wrapper = new QueryWrapper();
        String name = dto.getName();
        IPage<AdChannel> page = new Page(dto.getPage(), dto.getSize());
        wrapper.like(StringUtils.isNotEmpty(name), "name", name);
        adChannelMapper.selectPage(page, wrapper);
        List<AdChannel> records = page.getRecords();
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(records);

        return pageResponseResult;
    }

    @Override
    public ResponseResult saveChannel(AdChannel channel) {


        if (channel != null && channel.getId() != null && channel.getName() != null) {
            QueryWrapper<AdChannel> queryWrapper = new QueryWrapper();
            AdChannel adChannel = adChannelMapper.selectById(channel.getId());
            if (adChannel == null) {
                int insert = adChannelMapper.insert(channel);
                if (insert == 1) {
                    responseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
                    responseResult.setErrorMessage("操作成功");
                    return responseResult;
                }
            } else {
                responseResult.setCode(AppHttpCodeEnum.PARAM_INVALID.getCode());
                responseResult.setErrorMessage("操作失败");
            }
        }
        responseResult.setCode(AppHttpCodeEnum.PARAM_INVALID.getCode());
        responseResult.setErrorMessage("操作失败");
        return responseResult;
    }

    @Override
    public ResponseResult deleteChannel(Integer channelId) {
        if (channelId != null) {
            //判断是否存在，存在则删除否则告知不存在
            AdChannel adChannel = adChannelMapper.selectById(channelId);
            if (adChannel != null) {
                //删除时数据库中存在
                int i = adChannelMapper.deleteById(channelId);
                if (i == 1) {
                    //删除成功
                    responseResult.setErrorMessage("删除成功");
                    responseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
                    return responseResult;
                } else {
                    //删除失败
                    responseResult.setCode(AppHttpCodeEnum.SERVER_ERROR.getCode());
                    responseResult.setErrorMessage("删除失败，原因未知");
                }

            } //删除时数据库中不存在
            responseResult.setCode(AppHttpCodeEnum.DATA_NOT_EXIST.getCode());
            responseResult.setErrorMessage("数据不存在删除失败");
            return responseResult;
        }
        //传递过来要删除的ID为空
        responseResult.setCode(AppHttpCodeEnum.PARAM_INVALID.getCode());
        responseResult.setErrorMessage("参数错误");
        return responseResult;
    }

    @Override
    public ResponseResult updateChannel(AdChannel channel) {
        if (channel!=null){
                //来参有效
            AdChannel adChannel = adChannelMapper.selectById(channel.getId());
            if (adChannel!=null){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("id",channel.getId());
                int update = adChannelMapper.update(channel, queryWrapper);
                if (update==1){
                    responseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
                    responseResult.setErrorMessage("修改成功");
                    return responseResult;
                }
                responseResult.setCode(AppHttpCodeEnum.SERVER_ERROR.getCode());
                responseResult.setErrorMessage("修改失败，原因未知");
                return responseResult;
            }

            responseResult.setCode(AppHttpCodeEnum.DATA_NOT_EXIST.getCode());
            responseResult.setErrorMessage("数据不存在修改失败");
            return responseResult;
        }


            //参数为空
        responseResult.setCode(AppHttpCodeEnum.PARAM_INVALID.getCode());
        responseResult.setErrorMessage("参数为空");
        return responseResult;
    }


}